<?php


class PeopleController extends AppController {
    
    var $helpers = array('Html', 'Form', 'TinyMce');
    
    var $name = 'People';

    var $uses = array('WorkExperience', 'Person', 'Town', 'District', 'Region', 'User'); // specifico i modelli che utilizza il controller
    
    function beforeFilter() {
        //parent::beforeFilter();
        //$this->Auth->allow(array('list_all'));
    }
    
    function isAuthorized() {
        return true;
    }
    
    function index() {
        // se richiamato da countbox ritorno solo il numero di record
         if (isset($this->params['requested'])) {
            return $this->Person->find('count');
         }
         else {
            
            $this->set('people', $this->Person->find('all')); // view variable
         }
    }
    
    function list_all() {
        // se richiamato da countbox ritorno solo il numero di record
         if (isset($this->params['requested'])) {
            return $this->Person->find('count');
         }
         else {
            
            $this->set('people', $this->Person->find('all')); // view variable
         }
    }
    
    function add($username = '') {
        
        $user = $this->User->find('first',
                                     array(
                                         'conditions' => array(
                                             'User.username' => $username
                                         )
                                     )
                                );
        $user_id = $user['User']['id'];
        
        if (!empty($this->data) && $this->Person->validates()){
            
            if ($this->Person->save($this->data)) {
                $this->Session->setFlash('Informazioni personali salvate');
                $this->redirect(array(
                        'controller' => 'WorkExperiences', 
                        'action' => 'add', $this->Person->id));
            }
        }

        $this->set('regions', $this->Region->find('list'));
        $this->set('user_id', $user_id);
        $this->set('email', $username);
    }
    
    function edit($id) {
        
        // if $_POST is empty read the information from DB
         if(empty($this->data)) {
             $this->Person->id = $id;
             $this->data = $this->Person->read();
             $this->set('regions', $this->Region->find('list'));
         }
         // if $_POST != empty save the information
         else {
             if($this->Person->save($this->data)) {
                 $this->Session->setFlash("Le informazioni personali sono state aggiornate");
                 $this->redirect(array('action' => 'index'));
             }
         }
        
    }
    
    function edit_personal($user_id = '') {
        
        if($user_id == '') 
            $user_id = $this->Auth->user('id');
        
        if($user_id != '') {
            $person_info = $this->Person->find('first',array(
                                            'conditions' => array(
                                                'Person.user_id' => $user_id
                                            )
            ));
            
            $this->set('person_id', $person_info['Person']['id']);
            $this->set('user_id', $user_id);
        }
        else {
            $this->Session->setFlash('Utente non trovato');
            $this->redirect(array(
                    'controller' => 'pages',
                    'action' => 'home'
            ));
        }
        
    }
    
    function delete($id) {
        $this->Person->WorkExperience->deleteAll(array('WorkExperience.person_id' => $id));
        $this->Person->EducationExperience->deleteAll(array('EducationExperience.person_id' => $id));
        $this->Person->LanguageExperience->deleteAll(array('LanguageExperience.person_id' => $id));
        $this->Person->delete($id);
        $this->Session->setFlash('Il curriculum con id '.$id.' è stato eliminato');
        $this->redirect(array('action' => 'index'));
    }
    
}

?>
